<template>
  <FormDialog :formOption="formOption" :form="form" :rule="rule" ref="dialogRef" :reload="reload"/>
</template>

<script setup lang="ts">
import FormDialog from "~/components/common/FormDialog.vue";
import {
  useApi
} from "~/composeables";
import {FormFieldType, FormOption} from "~/models/data/form";
import BookCategoryCache from "~/store/booksvr/bookCategoryCache";
import DepartmentCache from "~/store/system/departmentCache";
import BookStatusCache from "~/store/booksvr/bookStatusCache";
import RoleCache from "~/store/system/roleCache";

const props = defineProps<{
  reload: Function,
}>();

const api = useApi();

const dialogRef = ref<any>(null);

const submitFunc = (model : any)=>{
  api.systemUserApi.update(model)
      .then(res => {
        if (res.success) {
          ElMessage.success("提交数据成功！");
          if (props.reload) {
            props.reload();
          }
          close();
        } else {
          ElMessage.error("数据提交失败,请重新尝试！");
        }
      })
      .catch(err => {
        console.error(err);
        ElMessage.error("数据提交失败");
      })
}

const formOption = ref<FormOption>({
  title: '修改用户信息',
  width: 480,
  labelWidth: 120,
  fieldList: [
    {
      fieldName: 'username',
      verboseName: '用户名',
      fieldType: FormFieldType.input,
      readonly: true,
    },
    {
      fieldName: 'password',
      verboseName: '密码',
      fieldType: FormFieldType.password,
    },
    {
      fieldName: 'realname',
      verboseName: '真实姓名',
      fieldType: FormFieldType.input,
    },
    {
      fieldName: 'idMilitary',
      verboseName: '军人证号',
      fieldType: FormFieldType.input,
    },
    {
      fieldName: 'departmentId',
      verboseName: '所属单位',
      fieldType: FormFieldType.cascader,
      selectSource: DepartmentCache.departmentTreeSelectSource,
      isMultiSelect: false
    },
    {
      fieldName: 'roles',
      verboseName: '用户身份',
      fieldType: FormFieldType.cascader,
      selectSource: RoleCache.roleSelectSource,
      isMultiSelect: true
    }
  ],
  submitFunc: submitFunc,
});

const form: any = ref({
  name: "",
  password: "",
  realname: "",
  idMilitary: null,
  departmentId: null,
  roles: [],
});

const rule = []

const open = (model: any)=>{
  dialogRef.value.open(JSON.parse(JSON.stringify(model)));
}

const close = ()=>{
  dialogRef.value.close();
}

defineExpose({open, close, dialogRef})

</script>